package com.echo.usedsfu;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.logging.Logger;

import javax.jdo.PersistenceManager;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.fileupload.FileItemIterator;
import org.apache.commons.fileupload.FileItemStream;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.io.IOUtils;

import com.echo.usedsfu.entity.MyImage;
import com.echo.usedsfu.persistence.PMF;
import com.google.appengine.api.datastore.Blob;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.Query;

public class UploadImgServlet extends HttpServlet{
	private static final Logger log = Logger.getLogger(UploadImgServlet.class.getName());
	
	public void doPost(HttpServletRequest req, HttpServletResponse resp)throws IOException{
		ServletFileUpload upload = new ServletFileUpload();
		try{
			FileItemIterator iter = upload.getItemIterator(req);

			FileItemStream imageItm = iter.next();
			InputStream imgStream = imageItm.openStream();
			
			Blob imageBlob = new Blob(IOUtils.toByteArray(imgStream));
			MyImage myImage = new MyImage(imageItm.getName(), imageBlob);
			
			PersistenceManager pm = PMF.get().getPersistenceManager();
			pm.makePersistent(myImage);
			pm.close();
			
			HttpSession session = req.getSession();
			session.setAttribute("img_id", myImage.getImg_id().toString());
			resp.sendRedirect("/postAd.jsp");
//			resp.setContentType("text/plain");
//			resp.getOutputStream().write("OK!".getBytes());
//			imageFor(imageItm.getName(), resp);
			
		}catch(FileUploadException e){
			e.printStackTrace();
		}
		
	}
	
	void imageFor(String name, HttpServletResponse resp){
		try{
			PersistenceManager pm = PMF.get().getPersistenceManager();
			String query = "select from "+MyImage.class.getName();
			List<MyImage> results = (List<MyImage>)pm.newQuery(query).execute();
			Blob image = ((MyImage)results.get(0)).getImage();
			
			resp.setContentType("image/jpeg");
			resp.getOutputStream().write(image.getBytes());
		}catch(IOException e){
			e.printStackTrace();
		}
	}
	
	public void imageFor(HttpServletRequest req,  HttpServletResponse resp)throws IOException{
		try{
			String img_id = req.getParameter("img_id");
			PersistenceManager pm = PMF.get().getPersistenceManager();
			Key k = KeyFactory.createKey(MyImage.class.getSimpleName(), Long.parseLong(img_id));
//			List<MyImage> results = (List<MyImage>)pm.newQuery(query).execute();
			Blob image = ((MyImage)pm.getObjectById(MyImage.class, k)).getImage();
			
			resp.setContentType("image/jpeg");
			resp.getOutputStream().write(image.getBytes());
		}catch(IOException e){
			e.printStackTrace();
		}
	}
}
